<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>区域规则(按件)管理</title>
</head>
<body>
    <div ng-app="myApp" ng-controller="PieceCtrl">
        <div class="row">
            <ui-container url="rule/piece/list" key="piece-list-00">
                <ui-search-form>
                    <search-region  class="col-md-6" label="地区" p-name="province" c-name="city" a-name="county"></search-region>
                    <search-select class="col-md-3" name="direction">
                        <option value="">请选择运单配送类型</option>
                        <option value="0">配送 </option>
                        <option value="1">上门换</option>
                        <option value="2">上门退</option>
                    </search-select>
                    <search-select class="col-md-3" name="settlementType">
                        <option value="">请选择价格类型</option>
                        <option value="0">自营</option>
                        <option value="1">合作站</option>
                        <option value="2">合作商</option>
                        <option value="3">大型合作商</option>
                    </search-select>
                    <search-select class="col-md-3" name="temperature">
                        <option value="">请选择运输温度等级</option>
                        <option value="0">常温</option>
                        <option value="1">生鲜冷链</option>
                    </search-select>
                    <search-select class="col-md-3" name="packageSize">
                        <option value="">请选择包裹大小规格</option>
                        <option value="1">小件</option>
                        <option value="2">大件</option>
                    </search-select>
                    <search-select class="col-md-3" name="expressScheduleType">
                        <option value="">请选择配送时效要求</option>
                        <option value="0">未知</option>
                        <option value="1">当日达</option>
                        <option value="2">次晨达</option>
                        <option value="3">三日达</option>
                        <option value="4">工作日</option>
                        <option value="5">节假日</option>
                        <option value="6">夜间配</option>
                        <option value="7">上午达</option>
                        <option value="8">下午达</option>
                    </search-select>
                    <search-select class="col-md-3" name="signer">
                        <option value="">请选择签收方式</option>
                        <option value="1">代签</option>
                        <option value="0">本人签收</option>
                    </search-select>
                    <search-select class="col-md-3" name="cod">
                        <option value="">请选择是否含有待收款</option>
                        <option value="1">是</option>
                        <option value="0">否</option>
                    </search-select>
                    <search-select-remote placeholder="请选择供应商" url="system/supplier/listall" class="col-md-3" name="supplierCode" value-name="code" text-name="name">
                    </search-select-remote>
                </ui-search-form>
                <ui-table-tool>
                    <a class="btn btn-sm btn-primary" href="javascript:void(0);" ng-click="addOpen('rule/piece/add.htm')" >新增</a>
                    <a class="btn btn-sm btn-primary" href="javascript:void(0);" onclick="reSet()">重置</a>
                </ui-table-tool>
                <ui-table>

                </ui-table>
                <table-page/>
            </ui-container>
        </div>
        <div class="row" id="piecetImport">
            <a  class="btn btn-sm btn-primary" href="javascript:void(0);" ng-click="exportDatas()" target="_blank" >下载模板</a>
            <a  class="btn btn-sm btn-success" href="./html/piecehelper.html" target="_blank" >导出帮助</a>
            <ui-upload url="rule/piece/import" success="uploadSuccess"></ui-upload>
        </div>
    </div>
<script>
    angular.module('myApp',['ui.table']);
    angular.module('myApp').controller('PieceCtrl', function($scope){
        $scope.uploadSuccess=function (returndata) {
            $("#piecetImport").find(".upload-result").remove();
            if(returndata.data["fileKey"]){
                $("#piecetImport").append('<a  class="btn btn-sm btn-success upload-result" href="rule/piece/down?fileKey='+returndata.data["fileKey"]+'" target="_blank" >下载导入失败的记录</a>');
            }
            toastr.success("总共"+returndata.data["totalNums"]+"条,成功"+returndata.data["successNums"]+"条");
        }
        $scope.exportDatas = function(){
            var params = {
                url: 'rule/piece/exporttemplate',
                type: "POST"
            };
            var exportForm = $("<form>");
            $(exportForm).css("display", "none");
            $(exportForm).attr('target', 'targetFrame');
            $(exportForm).attr('method', params.type);
            $(exportForm).attr('action', params.url);
            exportForm = exportForm.appendTo($("body"));
            exportForm.submit();
            exportForm.remove();
        }
        $scope.addOpen = function (url) {
            window.reloadTable = $scope['piece-list-00']._queryData;
            window.location.href=url;
        }
        function doDel(selectedItems) {
            const uiContainerCtrl=$scope['piece-list-00'];
            if(!selectedItems||selectedItems.length<=0){
                toastr.error("没有选中删除对象")
                return;
            }
            const ids=selectedItems.map(m => m.id);
            $.ajax({
                url:'rule/piece/delete',
                data:{ids:ids.join(",")}
            }).done(function (result) {
                if(result.code == '0000'){
                    toastr.success(result.msg);
                    uiContainerCtrl._queryData({});
                }else{
                    toastr.error(result.msg);
                }
            }).fail(function (r) {
                toastr.error("服务器异常"+r);

            });
        }

        $scope.addOrg = function (items) {
            console.info(items)
        }
        //每一列的操作按钮
        $scope._operateColumns=[{text:'编辑',click:function (data) {
            $scope.addOpen("rule/piece/update.htm?id="+data.id);
        }},{text:'删除',click:function (data) {
            doDel([data])
        }}]

        $scope._columns=[
            {name:'supplier.name',head:'供货商',render(data,value){
                if(data.supplier){
                    return data.supplier.name;
                }}},
            {name:'region.name',head:'地区',render(data,value){
                if(data.regionDetail){
                    switch (data.regionDetail.level){
                        case 3:
                            return data.regionDetail.provinceName +"|"+data.regionDetail.cityName+"|"+data.regionDetail.name;
                        case 2:
                            return data.regionDetail.provinceName +"|"+data.regionDetail.cityName;
                        case 1:
                            return data.regionDetail.provinceName;
                        default:
                            return"";
                    }
                }}},
            {name:'direction',head:'运单配送类型',render(data,value){
                switch(value+""){
                    case "0":
                        return '配送';
                    case "1":
                        return '上门换';
                    case "2":
                        return '上门退';
                    default:
                        return '';
                }}},
            {name:'temperature',head:'运输温度等级',render(data,value){
                switch(value+""){
                    case "0":
                        return '常温';
                    case "1":
                        return '生鲜冷链';
                    default:
                        return '';
                }}},
            {name:'expressScheduleType',head:'配送时效要求',render(data,value){
                switch(value+""){
                    case "1":
                        return '当日达';
                    case "2":
                        return '次晨达';
                    case "3":
                        return '三日达';
                    case "4":
                        return '工作日';
                    case "5":
                        return '节假日';
                    case "6":
                        return '夜间配';
                    case "7":
                        return '上午达';
                    case "8":
                        return '下午达';
                    default:
                        return  '';
                }}},
            {name:'packageSize',head:'包裹大小规格（当当）',render(data,value){
                switch (value+""){
                    case "1":
                        return '小件';
                    case "2":
                        return '中件';
                    case "3":
                        return '大件';
                    default:
                        "";
                }}},
            {name:'signer',head:'签收方式',render(data,value){
                switch (value+""){
                    case "0":
                        return '代签';
                    case "1":
                        return '本人签收';
                    default:
                        "";
                }}},
            {name:'settlementType',head:'价格类型',render(data,value){
                switch (value+""){
                    case "0":
                        return '自营';
                    case "1":
                        return '合作站';
                    case "2":
                        return '合作商';
                    case "3":
                        return '大型合作商';
                    default:
                        return '';
                }}},
            {name:'firstPrice',head:'首件/元'},

            {name:'secondPrice',head:'拖件/元'},
            {name:'cod',head:'是否含待收款',render(data,value){
                switch (value+""){
                    case "0":
                        return '否';
                    case "1":
                        return '是';
                    default:
                        "";
                }}},
        ];
        $scope._btns=[{text:'批量删除',click:doDel}];
    });
    function reSet() {
        var reSetObj = $('input');
        var selectObj = $('select');
        reSetObj.val("");
        selectObj.val("");
    }
</script>
</body>
</html>
